<template>
  <div class="page_main" v-loading="dataListLoading">
    <el-form :inline="true" :model="dataForm" size="small">
      <el-form-item>
        <el-button
          type="primary"
          @click="addOrUpdateHandle()"
          :disabled="!isAuth('system:menus:add')"
          >新增</el-button
        >
      </el-form-item>
    </el-form>
    <div style="flex:1" ref="myTable">
      <el-table
        :data="dataList"
        row-key="id"
        border
        style="width: 100%; "
        size="mini"
        v-if="tableHeight"
        :height="tableHeight"
      >
        <el-table-column
          prop="menuName"
          header-align="center"
          min-width="150"
          label="名称"
        >
        </el-table-column>
        <el-table-column
          prop="parentName"
          header-align="center"
          align="center"
          width="120"
          label="上级菜单"
        >
        </el-table-column>
        <el-table-column header-align="center" align="center" label="图标">
          <template slot-scope="scope">
            <icon-svg :name="scope.row.icon || ''"></icon-svg>
          </template>
        </el-table-column>
        <el-table-column
          prop="menuType"
          header-align="center"
          align="center"
          label="类型"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.menuType === 0" size="small">目录</el-tag>
            <el-tag
              v-else-if="scope.row.menuType === 1"
              size="small"
              type="success"
              >菜单</el-tag
            >
            <el-tag
              v-else-if="scope.row.menuType === 2"
              size="small"
              type="info"
              >按钮</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="orderNum"
          header-align="center"
          align="center"
          label="排序号"
        >
        </el-table-column>
        <el-table-column
          prop="url"
          header-align="center"
          align="center"
          width="150"
          :show-overflow-tooltip="true"
          label="菜单URL"
        >
        </el-table-column>
        <el-table-column
          prop="perms"
          header-align="center"
          align="center"
          width="150"
          :show-overflow-tooltip="true"
          label="授权标识"
        >
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              plain
              size="mini"
              @click="addOrUpdateHandle(scope.row.id)"
              :disabled="!isAuth('system:menus:update')"
              >修改</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./menu-add-or-update";
export default {
  data() {
    return {
      dataForm: {},
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      tableHeight: 0
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  mounted() {
    this.tableHeight = this.$refs.myTable.offsetHeight;
  },
  methods: {
    treeDataTranslate(arr) {
      if (Array.isArray(arr)) {
        return arr.map(({ list, ...rest }) => ({
          children: this.treeDataTranslate(list),
          ...rest
        }));
      }
      return [];
    },
    async getDataList() {
      this.dataListLoading = true;
      let [err, res] = await this.$Api.Menus.list();
      this.dataListLoading = false;
      if (!err) {
        this.dataList = this.treeDataTranslate(res);
      }
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    }
  }
};
</script>
